package collections;

import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.MutableClassToInstanceMap;
/**
 * 在Class 和 instance 之间建立映射关系的容器
 * @author tiangh
 * 2013-5-26 下午9:35:32
 */
public class ClassToInstanceMapDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		use();
	}
	
	public static void use() {
		//可以用在配置上，避免了ClassCast异常
		ClassToInstanceMap<IDAO> CONFIGUE=MutableClassToInstanceMap.create();
		CONFIGUE.putInstance(MySQLImpl.class, new MySQLImpl());
		CONFIGUE.putInstance(OracleImpl.class, new OracleImpl());
		
		IDAO dao = CONFIGUE.get(MySQLImpl.class);
		dao.connect();		
	}
	
	static interface IDAO{
		void connect();
	}
	
	public static  class MySQLImpl implements IDAO{
		@Override
		public void connect() {
			System.out.println("connect database with MySql");
		}
		
	}
	public static  class OracleImpl implements IDAO{
		@Override
		public void connect() {
			System.out.println("connect database with Oracle");
		}
	}
}
